﻿#ifndef _SERVER_NET_
#define _SERVER_NET_

#include <unordered_map>
#include "../../3rd/st_asio_wrapper/tcp/server.h"
#include "../../3rd/st_asio_wrapper/ext/tcp.h"
#include "../../3rd/libhserver/MacroHeads.h"
#include "server_base.h"

using namespace st_asio_wrapper;
using namespace st_asio_wrapper::tcp;
using namespace st_asio_wrapper::ext::tcp;

class CServerNet;

class CNetBase : public server_base<server_socket, object_pool<server_socket>, i_server>
{
public:
	CNetBase(service_pump& pump, CServerNet& server_net):
		server_base<server_socket, object_pool<server_socket>, i_server>(pump), server_net_(server_net){}
	~CNetBase(){}
public:
	// 从网络层往上层传出来的玩家掉线事件
	virtual void del_client(unsigned long uid);
	virtual void packageStrerm(std::string& msg, int64_t uid);
	void broadcast_msg_to_client(uint32_t msgId, std::string& msg, const std::string& key);

	boost::shared_ptr<timer> GetSharedPtrBySocketId(unsigned long uid);
	bool send_by_uid(const UINT mid, std::string& msg, uint32_t msgId, const std::string& key);
private:
	CServerNet& server_net_;
};

class CServerNet
{
public:
	CServerNet(IService* server, service_pump& pump);
	~CServerNet(void);
public:
	void NetStop();
	void packageStrerm(std::string& msg, int64_t uid);
	int  client_msg_dispatch(std::string& msg, unsigned long uid, uint32_t msgId);
	bool send_by_uid(const UINT mid, std::string& msg, uint32_t msgId);
	void broadcast_msg_to_client(uint32_t msgId, std::string& msg);
	bool is_connnected(uint32_t);

	std::string getKey(){return mMyKey;	}

	void setKey(std::string key);

	void OnInit(UShort port);

	boost::shared_ptr<timer> GetSharedPtrBySocketId(unsigned long uid);

	CNetBase& GetNetBase() { return net_base; }

	// 从网络层往逻辑层抛出的玩家掉线事件
	void del_client(unsigned long uid);
	// 主动关闭一个客户端连接
	void close_client(unsigned long uid);
private:
	std::string mMyKey;
	CNetBase net_base;
	IService* service_;
	service_pump& m_pump;
};

#endif